package com.ccyy.manage.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccyy.manage.model.BaseResponse;
import com.ccyy.manage.model.entity.Menu;
import com.ccyy.manage.model.entity.Role;
import com.ccyy.manage.service.MenuService;
import com.ccyy.manage.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 是超超越越啊~
 * @since 2021-07-20
 */
@Api("菜单模块")
@RestController
@RequestMapping("/menu")
@RequiresAuthentication
public class MenuController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private MenuService menuService;

    @ApiOperation("查询当前用户菜单")
    @GetMapping("/list")
    @ResponseBody
    public BaseResponse getMenu(HttpServletRequest request, HttpServletResponse response) {
        String token = WebUtils.toHttp(request).getHeader("Authorization");
        String username = jwtUtil.getClaimByToken(token).getSubject();
        List<Menu> menus = menuService.selectCurrentUserMenuTree(username);
        return BaseResponse.success(menus);
    }

    @ApiOperation("新增菜单")
    @PostMapping
    @ResponseBody
    public BaseResponse add(Menu menu) {
        menuService.save(menu);
        return BaseResponse.success();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{menuId}")
    @ResponseBody
    public BaseResponse delete(@PathVariable("menuId") Long menuId) {
        menuService.deleteByIDAndChildren(menuId);
        return BaseResponse.success();
    }

    @ApiOperation("获取单个菜单的信息")
    @GetMapping("/{menuId}")
    public BaseResponse updateMenu(@PathVariable("menuId") Long menuId) {
        return BaseResponse.success(menuService.getById(menuId));
    }

    @ApiOperation("修改菜单")
    @PutMapping
    @ResponseBody
    public BaseResponse update(Menu menu) {
        menuService.updateById(menu);
        return BaseResponse.success();
    }

}
